package com.liandu.common.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Component
@Aspect
@Slf4j
public class LogAop {

    @Pointcut("execution(* com.liandu.controller.*.*(..))")
    public void point(){}

    @Before(value = "point()")
    public void before(JoinPoint jp){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = null;
        if (attributes != null) {
            request = attributes.getRequest();
            log.info("<uri> ==> {},<method> ==> {},<ip> ==> {}", request.getRequestURI(), request.getMethod(), request.getRemoteAddr());
            log.info("CLASS_METHOD ==> " + jp.getSignature().getDeclaringTypeName() + "." + jp.getSignature().getName());
            log.info("parameters == > {}",Arrays.toString(jp.getArgs()));
        }
    }
}
